﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Drawing;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;

namespace BasePlatformCommon
{
	[JsonObject(MemberSerialization.OptIn)]
	public class Lamp : Device
	{
		[JsonProperty]
		public PInfoData ProductionInfo { get; set; }	// 产品信息

		[JsonProperty]
		public int TotalChannel { get; set; }			// 总管脚数

		[JsonProperty]
		public int LampIndex { get; set; }				// 链路上第几个灯

		[JsonProperty]
		public int StartChannel { get; set; }			// 灯具起始位置

		[JsonProperty]
		public float Current { get; set; }				// 灯具电流

		[JsonProperty]
		public float Voltage { get; set; }				// 灯具电压

		[JsonProperty]
		public int Temperature { get; set; }			// 灯具温度

		[JsonProperty]
		public int CommState { get; set; }				// 灯具通信状态 0-正常 1-异常

		[JsonProperty]
		public int TotalComm { get; set; }				// 总包数

		[JsonProperty]
		public int ErrorComm { get; set; }				// 错误包数

		[JsonProperty]
		public List<int> ChannelStateList { get; set; }	// 管脚状态数据集合 0-正常 1-异常

		// 构造函数
		public Lamp()
		{
			deviceType = DeviceType.Lamp;

			this.name = "灯点";
			if (LanguageOper.available == true)
			{
				this.name = LanguageOper.ReadValue("Chain", "lamp.name");
			}

			ProductionInfo = new PInfoData();
			TotalChannel = 12;	// 默认芯片管脚数为12
			LampIndex = 0;
			StartChannel = 0;
			Current = 0;
			Voltage = 0;
			Temperature = 0;
			CommState = 0;
			TotalComm = 0;
			ErrorComm = 0;
			ChannelStateList = new List<int>();
		}

		#region Json交互相关操作

		// 设置灯具数据
		public void setLampData(JObject jsonData)
		{
			parseLampData(jsonData);
		}

		// 解析灯具数据
		private void parseLampData(JObject jsonData)
		{
			try
			{
				// 通用属性
				ProductionInfo.Manufacturer = (string)jsonData["Manufacturer"];
				ProductionInfo.ProductionDate = (string)jsonData["ProductionDate"];
				ProductionInfo.ProductionType = (string)jsonData["ProductionType"];
				ProductionInfo.SerialNumber = (string)jsonData["SerialNumber"];
				ProductionInfo.Version = (string)jsonData["Version"];

				// 独立属性
				TotalChannel = (int)jsonData["TotalChannel"];
				LampIndex = (int)jsonData["LampIndex"];
				Current = (float)jsonData["Current"];
				Voltage = (float)jsonData["Voltage"];
				Temperature = (int)jsonData["Temperature"];
				CommState = (int)jsonData["CommState"];
				TotalComm = (int)jsonData["TotalComm"];
				ErrorComm = (int)jsonData["ErrorComm"];

				try
				{
					parseChannelState((JObject)jsonData["ChannelState"]);
				}
				catch
				{
					Log.DebugLogger.Error("ChannelState Parse Error!");
				}
			}
			catch
			{
				Log.DebugLogger.Error("Lamp Parse Error!");
			}
		}

		// 解析灯具管脚状态、通信状态以及通道状态
		private void parseChannelState(JObject jsonData)
		{
			// 解析灯具管脚状态
			for (int channelIndex = 0; channelIndex < TotalChannel; channelIndex++)
			{
				string channelNameStr = "Channel" + channelIndex.ToString();
				try
				{
					ChannelStateList.Add((int)jsonData[channelNameStr]);
				}
				catch
				{
					Log.DebugLogger.Error("Channel " + channelNameStr + " Parse Error!");
				}
			}
		}

		#endregion
		
		// 拼接提示信息
		public string buildTipMessage()
		{
			string formatStr = "总包数 {0}, 错误包数 {1}";
			if (LanguageOper.available == true)
			{
				formatStr = LanguageOper.ReadValue("Chain", "lamp.formatStr");
			}

			StringBuilder sb = new StringBuilder();

			sb.Append(String.Format(formatStr, TotalComm, ErrorComm));

			tipMessage = sb.ToString();
			return tipMessage;
		}
	
	}
}
